﻿using DemoUser.Core.User;
using Furion.ClayObject.Extensions;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar.Extensions;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace DemoUser.Application.User.Filter
{
    public class MyActionFilter : IAsyncActionFilter
    {

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //============== 这里是执行方法之后获取数据 ====================
            //List<TbUser> list = new List<TbUser>();
            var actionContext = await next();
            //Console.WriteLine(actionContext);
            // 获取返回的结果
            var returnResult = actionContext.Result;
            //Console.WriteLine(returnResult);
            //Type type = returnResult.GetType();
            //Console.WriteLine(type.Name);
            //对返回值进行转换
            //Console.WriteLine(returnResult.ToJson());
            //获取返回值的json数据
            string returnJson = returnResult.ToJson();
            if (returnJson != null)
            {
                //将返回值的json数据转换为ObjectResult类型的数据
                ObjectResult result = JsonConvert.DeserializeObject<ObjectResult>(returnJson);
                //获取ObjectResult类型数据的Value值
                Object rv = result.Value;
                Type rvt = rv.GetType();
                //Console.WriteLine(rvt.Name);
                //ObjectResult类型数据的Value值转化为json数据
                string rvstr = rv.ToJson();
                //Console.WriteLine(rv.ToJson());
                //将ObjectResult类型数据的Value值转化为的json数据转化为List<Object>类型的数据
                List<Object> rs = null;
                Object rsi = null;
                if (rvt.Name == "JArray")
                {
                    rs = JsonConvert.DeserializeObject<List<Object>>(rvstr);
                    Console.WriteLine("列表数据");
                    for (int i = 0; i < rs.Count; i++)
                    {
                        Type rst = rs[i].GetType();
                        //Console.WriteLine(rst.Name);
                        //Object rsto = Activator.CreateInstance(rst);
                        //Type rstot = rsto.GetType();
                        //Console.WriteLine(rsto);
                        Console.WriteLine(rs[i].ToJson());
                        //修改数据
                        UpdateData(rst);
                    }
                }
                else
                {
                    rsi = JsonConvert.DeserializeObject<Object>(rvstr);
                    Console.WriteLine("单例数据");
                    Type rsit = rsi.GetType();
                    //Console.WriteLine(rsit.Name);


                    //修改数据
                    UpdateData(rsit);
                    
                }

               
                    //string rsi = rs[i].ToJson();
                    //TbUser tb = JsonConvert.DeserializeObject<TbUser>(rsi);
                    //Type type = tb.GetType();
                    //Object o = Activator.CreateInstance(type);
                
                //将列表数据返回给ObjectResult类型数据的Value
                //Console.WriteLine(result.ToJson());
                //将result返回给返回结果
                //returnResult = result;
            }
            //将返回结果返回给数据获取原始值
            //actionContext.Result = returnResult;
        }


        //修改数据
        private static void UpdateData(Type type) {
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo prop in properties)
            {
                if (prop.PropertyType == typeof(int) && prop.Name != "Id")
                {
                    //修改数据
                    Console.WriteLine(prop.PropertyType);
                }
            }
        }
    }
}
